// pages/register/register.js

let host = "https://ymy138.cn/base/"
//let host = "http://127.0.0.1:1001//base/"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgurl:"",
    username: '',
    nickname: '',
    title: wx.getStorageSync('sysname')+"-登录",
    qidText: '',
    qidValue: [],
    qidTitle: '',
    qids: [],
    qidVisible: false,

    pidText: '',
    pidValue: [],
    pidTitle: '',
    pids: [],
    pidVisible: false,

    uidText: '',
    uidValue: [],
    uidTitle: '',
    uids: [],
    uidVisible: false,

  },


  /**
   * 处理用户名输入
   */
  handleUsernameInput(event) {
    console.log("handleUsernameInput",event)
    this.setData({
      username: event.detail.value
    });
  },

  /**
   * 处理密码输入
   */
  handleNicknameInput(event) {
    this.setData({
      nickname: event.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this

    wx.request({
      url: host+'api.Login/getLoginBg',
      method:"GET",
      success:function (res) {
        that.setData({
          bgurl:res.data.data
        })
      }
    });

    wx.request({
      url: host+'api.Login/getPickers',
      method:"GET",
      success:function (res) {
        let data = res.data.data;
        that.setData({
          qids:data.qids,
          pids:data.pids,
          uids:data.uids
        })
      }
    });


  },

  onQIDPicker() {
    this.setData({ qidVisible: true, qidTitle: '选择所在区域' });
  },

  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;

    console.log('picker change:', e.detail);
    console.log('picker change11:', e.currentTarget.dataset);
    if(key=='uid'){
      this.getPids(e.detail.value[0])
    }
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: e.detail.label[0],
    });
  },

  onPIDPicker() {
    this.setData({ pidVisible: true, pidTitle: '选择所在项目' });
  },



  onUIDPicker() {
    this.setData({ uidVisible: true, uidTitle: '选择所在单位' });
   // this.getPids(this.data.uidValue[0])
  },

  getPids(pid){
    let that = this
    wx.request({
      url: host+'api.Login/getPids',
      method:"GET",
      data:{
        pid:pid
      },
      success:function (res) {
        let data = res.data.data
        that.setData({
          pids:data,
          pidText: '',
          pidValue: [],
        })
      }
    })
  },

  submit() {
    let that = this
    let username = that.data.username
    let nickname = that.data.nickname
    let qid = that.data.qidValue[0]
    let pid = that.data.pidValue[0]
    let uid = that.data.uidValue[0]
    if(username==''||nickname==''||qid==''||pid==''||uid==''){
      wx.showToast({
        title: '请填写完整信息',
        icon:'none'
      })
    }else{
      wx.request({
        url: host+'api.Login/register',
        method:"POST",
        data:{
          username:username,
          nickname:nickname,
          Q_ID:qid,
          U_ID:uid,
          P_ID:pid,
        },
        success:function (res) {
          let data = res.data
          wx.showToast({
            title: data.info,
            icon:'none',
            duration:2000,
            complete:function () {
              //延迟跳转
              if(data.code==1){
                setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/login/login',
                  })
                },2000)
              }
            }
          })

        }
      })
    }

  },



})
